# Curl useful commands

This is a cheatsheet taken from the [how to curl better](https://conf.tube/w/qNm5tMUqHnGZPbtCyhxnUd).

## Simplest way to `GET` data

```bash
$ curl example.com
```

Show response header

```bash
$ curl -i https://example.com
```

`GET` json and show it nicely

```bash
$ curl https://example.com/json | jq
```

`HEAD` only shows the response headers

```bash
$ curl -I https://example.com
```

Please follow redirects

```bash
$ curl -I -L https://example.com/redirected
```

## URL Globbing

```bash
$ curl https://example.com/[1-9].html
```

```bash
$ curl https://example.com/[01-99].html
```

```bash
$ curl https://example.com/[a-z].html
```

Provide a step when globbing

```bash
$ curl https://example.com/[1-9:2].html
```

```bash
$ curl https://example.com/[a-z:3].html
```

Save the matching files to output with `#1` -> [1-9] parameter

```bash
$ curl https://example.com/[1-9].html -o save_#1.html
```

Comma separated strings

```bash
$ curl https://example.com/{ham,cheese,pineapple}.jpg -o hawaii_#1.jpg
```

Combine everything in one line

```bash
$ curl https://example.com/issue[1996-1999]/vol[1-4]/part{a,b,c}.html
```

## Verbose shows more from under the hood

```bash
$ curl -v https://example.com/ -o /dev/null
```

## Pass in custom headers

```bash
$ curl https://example.com/ -H "User-Agent: Some Silly Agent"
$ curl https://example.com/ -H "Magic: disc0"
$ curl https://example.com/ -H "User-Agent:"
$ curl https://example.com/ -H "User-Agent;"
```

## Methods

POST some basic data to the remote

```bash
$ curl -d name=Daniel -i https://example.com/receiver
```

POST a file

```bash
$ curl -d @file https://example.com/receiver -o saved
# Post a standard input
$ ls -l | curl -d @- https://example.com/receiver -o saved
# Post as binaries
$ ls -l | curl --data-binary @- https://example.com/receiver
# Post json as binary
$ curl --data-binary @file.json -H "Content-Type: application/json" https://example.com
```

PUT a file

```bash
$ curl -T localfile -i https://example.com/remote_name
```

Change the method string

```bash
# Use -X if you want a different menthod than curl would use
curl -T localfile -X SWOOSH https://example.com/remote_name -o save
```

## Cookies

Save cookies from site

```bash
$ curl -c cookiejar.txt https://example.com/
```

Send cookies to the server

```bash
$ curl -b cookiejar.txt https://example.com/
```

Cookies in a login

```bash
$ curl -b cookiejar.txt -c cookiejar.txt https://example.com/login -d user=daniel -d password=1234
# Request data as a logged in user
$ curl -b cookiejar.txt -c cookiejar.txt https://example.com/profile
```
